    /******************************************************************************
* SVN­Version­Information:
* ­­­­­­­­­­­­­­­­­­­­­­­­---------------------
* $LastChangedRevision: 6 $
* $LastChangedBy: c.a.duevel $
* $LastChangedDate: 2009-08-26 15:12:37 +0000 (Wed, 26 Aug 2009) $
* $HeadURL: http://robofight.googlecode.com/svn/trunk/ClientJavaFX/src/de/hft_stuttgart/robofight/client/javafx/BotShop.fx $
******************************************************************************/
package de.hft_stuttgart.robofight.client.javafx;

import de.hft_stuttgart.robofight.client.java.JBotShop;
import de.hft_stuttgart.robofight.client.java.JPlayer;
import de.hft_stuttgart.robofight.client.javafx.Constants;
import de.hft_stuttgart.robofight.client.javafx.util.*;
import java.util.List;
import javafx.ext.swing.SwingLabel;
import javafx.scene.CustomNode;
import javafx.scene.Group;
import javafx.scene.input.MouseEvent;
import javafx.scene.Node;
import javafx.scene.paint.Color;
import javafx.scene.control.ListView;
import javafx.scene.Cursor;
import javafx.scene.control.Button;
import javafx.scene.control.TextBox;
import com.sun.javafx.scene.control.caspian.TextBoxSkin;


import javafx.scene.shape.Rectangle;


/**
 * New botShop. To buy, sell and administrate the bots.
 * @author michael.rieker@web.de
 */

public class BotShop extends CustomNode{

    package var getNodeByName : function(nodeName : String) : Node;
    package var getPlayer : function() : JPlayer;
    public-init var jBotShop = new JBotShop(getPlayer());
    package var switchNode : function(className : String) : Void;
    public var allChassis : java.util.List;
    public var allShields : java.util.List;
    public var allArmaments : java.util.List;
    public var allSpecialItems : java.util.List;
    public var allRobots : java.util.List;
    public var allRobotItems : java.util.List;
    public var options = ["Sale", "Cancel"];
    public var botShopActive : Boolean = false;
    public var chassis : String = "0";
    public var shield : String = "0";
    public var armament : String = "0";
    public var currentPrice : String = "0";
    public var showCoinsOfPlayer : String = jBotShop.getCoins().toString();

    // Textbox to enter the name of the robot
    public var robotName = TextBox {
        var fxzLayout = getNodeByName("robotNameTextfield").layoutBounds;
        layoutX: fxzLayout.minX + 5,
        layoutY: fxzLayout.minY + 7,
        font : Constants.FONT_TEXTFIELD,
        height : 16
        width: 150
        skin: TextBoxSkin {
            backgroundFill: Color.rgb(11, 17, 16)
            borderFill: Color.rgb(11, 17, 16)
            caretFill: Color.WHITE
            focusFill: Color.rgb(11, 17, 16)
            highlightFill: Color.rgb(11, 17, 16)
            promptTextFill: Color.rgb(11, 17, 16)
            selectedTextFill: Color.BLUE
            shadowFill: Color.rgb(11, 17, 16)
            textFill: Color.WHITE
            cornerRadius: 15
        }
    }

    //Displays the current amount of the coins of a player
    public var coins = SwingLabel{
        var fxzLayout = getNodeByName("currentCoins").layoutBounds;
        layoutX: fxzLayout.minX + 70,
        layoutY: fxzLayout.minY + 7,
        text: bind showCoinsOfPlayer
        foreground: Color.WHITE;
        font : Constants.FONT_TEXTFIELD;
        width : 50;
    }

    // Group to display all bots of a player.
    public var showCurrentRobot = Group {
        var fxzLayout = getNodeByName("Rahmen_currentBot").layoutBounds;
        layoutX : fxzLayout.minX + 20,
        layoutY : fxzLayout.minY + 70,
        content : []
    }

    // Displays the current selected chassisname
    public var showCurrentBotChassis = SwingLabel{
        var fxzLayout = getNodeByName("currentBot_maxWeight").layoutBounds;
        layoutX: fxzLayout.minX + 120,
        layoutY: fxzLayout.minY - 5,
        text : bind chassis;
        font : Constants.FONT_TEXTFIELD
        foreground: Color.WHITE
        width: 100
    }

    // Displays the current selected shieldname
    public var showCurrentBotShield = SwingLabel{
        var fxzLayout = getNodeByName("currentBot_health").layoutBounds;
        layoutX: fxzLayout.minX + 129,
        layoutY: fxzLayout.minY - 5,
        text : bind shield;
        font : Constants.FONT_TEXTFIELD
        foreground: Color.WHITE
        width: 100
    }

    // Displays the current selected armamentname
    public var showCurrentBotArmament = SwingLabel{
        var fxzLayout = getNodeByName("currentBot_firePower").layoutBounds;
        layoutX: fxzLayout.minX + 101,
        layoutY: fxzLayout.minY - 5,
        text : bind armament;
        font : Constants.FONT_TEXTFIELD
        foreground: Color.WHITE
        width: 100
    }

    // Displays the current value of the bot
    public var showCurrentPrice = SwingLabel {
        var fxzLayout = getNodeByName("currentBot_Price").layoutBounds;
        layoutX: fxzLayout.minX + 128,
        layoutY: fxzLayout.minY - 5,
        text : bind currentPrice
        font : Constants.FONT_TEXTFIELD
        foreground: Color.WHITE
        width: 100
    }

    // Updates the botview with the current selected items
    function updateBotView() : Void {
        currentPrice = (jBotShop.getAmountChassis() + jBotShop.getAmountShield() + jBotShop.getAmountArmament()).toString();
        showCurrentRobot.content = null;
        insert getNodeByName(jBotShop.getChassis()) into showCurrentRobot.content;
        insert getNodeByName(jBotShop.getShield()) into showCurrentRobot.content;
        insert getNodeByName(jBotShop.getArmament()) into showCurrentRobot.content;
        chassis = jBotShop.getMaxWeight().toString();
        shield = jBotShop.getHealthOfShield().toString();
        armament = jBotShop.getFirePower().toString();
    }

    // On click on a robot in robotlistview displays bot details in current view
    function showBotDetails() : Void {
        chassisListView.select(-1);
        shieldListView.select(-1);
        armamentListView.select(-1);
        botShopActive = true;
        clearAllSelectedItemsProperties();
        jBotShop.setChassisSelected(true);
        jBotShop.setChassis((jBotShop.getAllRobots().get(robotListView.selectedIndex) as java.util.ArrayList).get(2) as String);
        jBotShop.setMaxWeight((jBotShop.getAllRobots().get(robotListView.selectedIndex) as java.util.ArrayList).get(6) as Integer);
        jBotShop.setShield((jBotShop.getAllRobots().get(robotListView.selectedIndex) as java.util.ArrayList).get(3) as String);
        jBotShop.setHealthOfShield((jBotShop.getAllRobots().get(robotListView.selectedIndex) as java.util.ArrayList).get(7) as Integer);
        jBotShop.setArmament((jBotShop.getAllRobots().get(robotListView.selectedIndex) as java.util.ArrayList).get(4) as String);
        jBotShop.setFirePower((jBotShop.getAllRobots().get(robotListView.selectedIndex) as java.util.ArrayList).get(8) as Integer);
        jBotShop.setAmountChassis((jBotShop.getAllRobots().get(robotListView.selectedIndex) as java.util.ArrayList).get(9) as Integer);
        jBotShop.setAmountShield((jBotShop.getAllRobots().get(robotListView.selectedIndex) as java.util.ArrayList).get(10) as Integer);
        jBotShop.setAmountArmament((jBotShop.getAllRobots().get(robotListView.selectedIndex) as java.util.ArrayList).get(11) as Integer);
        updateBotView();
    }

    // Listview contains all bots of a player
    var robotListView : ListView = ListView {
        var fxzLayout = getNodeByName("Rahmen_ownBots").layoutBounds;
        height: 130;
        width : 312
        layoutX : fxzLayout.minX +20,
        layoutY : fxzLayout.minY + 53,
        items : jBotShop.getAllRobotsAsArray();
        skin : Constants.getListViewSkin();
        onMouseClicked: function(e : MouseEvent) : Void{
            showBotDetails();
        }
    }

    // Listview  contains all chassis
    var chassisListView : ListView = ListView {
        height: 200;
        width : 225;
        items : jBotShop.getNameOfChassis();
        layoutX : getNodeByName("setChassis").layoutBounds.minX;
        layoutY : getNodeByName("setChassis").layoutBounds.minY + 55;
        onMouseClicked: function(e : MouseEvent) : Void{
            setChassis(chassisListView.selectedIndex);
        }
        skin : Constants.getListViewSkin();
    }

    // Listview contains all shields
    var shieldListView : ListView = ListView {
        height: 200;
        width : 225;
        items : jBotShop.getNameOfShields();
        layoutX : getNodeByName("setShield").layoutBounds.minX;
        layoutY : getNodeByName("setShield").layoutBounds.minY + 55;
        onMouseClicked: function(e : MouseEvent) : Void{
            setShield(shieldListView.selectedIndex);
        }
        skin : Constants.getListViewSkin();
    }

    // Listview contains all armaments
    var armamentListView : ListView = ListView {
        height: 200;
        width : 260;
        items : jBotShop.getNameOfArmaments();
        layoutX : getNodeByName("setArmament").layoutBounds.minX ;
        layoutY : getNodeByName("setArmament").layoutBounds.minY + 55;
        onMouseClicked: function(e : MouseEvent) : Void{
            setArmament(armamentListView.selectedIndex);
        }
        skin : Constants.getListViewSkin();
    }

    // Sets a selected chassis
    function setChassis(tmp : Integer) : Void {
        if(botShopActive){
            robotListView.select(-1);
            clearAllSelectedItemsProperties();
            botShopActive = false;
        }
        clearChassisProperties();
        allChassis = jBotShop.getAllChassis();
        var price : Integer = (allChassis.get(tmp) as java.util.ArrayList).get(1) as Integer;
        if( jBotShop.getDBCoinsMinusItems() >= price ){
            if(jBotShop.isShieldSelected() or jBotShop.isArmamentSelected()){
                shieldListView.select(-1);
                armamentListView.select(-1);
                clearShieldProperties();
                clearArmamentProperties();
            }
            jBotShop.setCoins(jBotShop.getAmountChassis());
            jBotShop.setCoins((price * (-1)));
            jBotShop.setChassis((allChassis.get(tmp) as java.util.ArrayList).get(0) as String);
            jBotShop.setMaxWeight((allChassis.get(tmp) as java.util.ArrayList).get(2) as Integer);
            jBotShop.setChassisSelected(true);
            jBotShop.setAmountChassis(price);
            showCoinsOfPlayer = jBotShop.getDBCoinsMinusItems().toString();
            updateBotView();
        } else {
            insert Message {
                    msg: "Sorry! Not enough coins to get this chassis!"
                    msgBoxBackground: getNodeByName("msgBoxmessageBox")

                    } into group.content;
                    chassisListView.select(-1);
                    shieldListView.select(-1);
                    armamentListView.select(-1);
        }
    }

    // Sets a selected shield
    function setShield(tmp : Integer) : Void {
        clearShieldProperties();
        allShields = jBotShop.getAllShields();
        if(botShopActive){
            robotListView.select(-1);
            clearAllSelectedItemsProperties();
            botShopActive = false;
        }
        var price : Integer = (jBotShop.getAllShields().get(tmp) as java.util.ArrayList).get(1) as Integer;
        var weight : Integer = (jBotShop.getAllShields().get(tmp) as java.util.ArrayList).get(2) as Integer;
        if(jBotShop.isChassisSelected()){
            jBotShop.setCoins(jBotShop.getAmountShield());
            if(jBotShop.getDBCoinsMinusItems() >= price){
                jBotShop.setCurrentWeight(-jBotShop.getWeightShield());
                var diff = jBotShop.getMaxWeight() - jBotShop.getCurrentWeight();
                if(diff >= weight){
                    if(jBotShop.isArmamentSelected()){
                        armamentListView.select(-1);
                        clearArmamentProperties();
                    }
                    jBotShop.setCoins((price * (-1)));
                    jBotShop.setAmountShield(price);
                    jBotShop.setWeightShield(weight);
                    jBotShop.setShield((jBotShop.getAllShields().get(tmp) as java.util.ArrayList).get(0) as String);
                    jBotShop.setHealthOfShield((jBotShop.getAllShields().get(tmp) as java.util.ArrayList).get(3) as Integer);
                    jBotShop.setShieldSelected(true);
                    showCoinsOfPlayer = jBotShop.getDBCoinsMinusItems().toString();
                    updateBotView();
                } else {
                    jBotShop.setCoins(jBotShop.getAmountShield());
                    jBotShop.setAmountShield(0);
                    jBotShop.setCurrentWeight(-jBotShop.getWeightShield());
                    jBotShop.setWeightShield(0);
                    jBotShop.setShieldSelected(false);
                    jBotShop.setHealthOfShield(0);
                    jBotShop.setShield("");
                    updateBotView();
                    insert Message {
                            msg: "The ultimate load of your current chassis can't carry this shield!"
                            msgBoxBackground: getNodeByName("msgBoxmessageBox")

                            } into group.content;
                    shieldListView.select(-1);
                    armamentListView.select(-1);
                }
            } else {
                shieldListView.select(-1);
                armamentListView.select(-1);
                insert Message {
                        msg: "Sorry! Not enough money to buy this shield!"
                        msgBoxBackground: getNodeByName("msgBoxmessageBox")
                } into group.content;
            }
         } else {
             shieldListView.select(-1);
             armamentListView.select(-1);
            insert Message {
                    msg: "First select a chassis!"
                    msgBoxBackground: getNodeByName("msgBoxmessageBox")

                    } into group.content;
         }
    }

    // Sets a selected armament
    function setArmament(tmp : Integer) : Void {
        clearArmamentProperties();
        allArmaments = jBotShop.getAllArmaments();
        if(botShopActive){
            robotListView.select(-1);
            clearAllSelectedItemsProperties();
            botShopActive = false;
        }
        var price : Integer = (jBotShop.getAllArmaments().get(tmp) as java.util.ArrayList).get(1) as Integer;
        var weight : Integer = (jBotShop.getAllArmaments().get(tmp) as java.util.ArrayList).get(2) as Integer;
        if(jBotShop.isShieldSelected()){
            jBotShop.setCoins(jBotShop.getAmountArmament());
            if(jBotShop.getDBCoinsMinusItems() >= price){
                var diff : Integer = jBotShop.getMaxWeight() - jBotShop.getWeightShield();
                if(diff >= weight){
                    jBotShop.setCoins((price * (-1)));
                    jBotShop.setAmountArmament(price);
                    jBotShop.setWeightArmament(weight);
                    jBotShop.setArmament((jBotShop.getAllArmaments().get(tmp) as java.util.ArrayList).get(0) as String);
                    jBotShop.setFirePower((jBotShop.getAllArmaments().get(tmp) as java.util.ArrayList).get(3) as Integer);
                    jBotShop.setArmamentSelected(true);
                    showCoinsOfPlayer = jBotShop.getDBCoinsMinusItems().toString();
                    updateBotView();
                } else {
                    jBotShop.setCoins(jBotShop.getAmountArmament());
                    jBotShop.setAmountArmament(0);
                    jBotShop.setWeightArmament(0);
                    jBotShop.setArmamentSelected(false);
                    jBotShop.setFirePower(0);
                    jBotShop.setArmament("");
                    updateBotView();
                    insert Message {
                            msg: "Check the maximum weight of your components!"
                            msgBoxBackground: getNodeByName("msgBoxmessageBox")

                            } into group.content;
                    armamentListView.select(-1);
                }
            } else {
                armamentListView.select(-1);
                insert Message {
                        msg: "Sorry! Not enough money to buy this component!"
                        msgBoxBackground: getNodeByName("msgBoxmessageBox")

                        } into group.content;
            }
        } else {
            armamentListView.select(-1);
            insert Message {
                    msg: "First select a shield!"
                    msgBoxBackground: getNodeByName("msgBoxmessageBox")

                    } into group.content;
        }
    }

    //Clears all selected values
    function clearAllSelectedItemsProperties() : Void{
        clearChassisProperties();
        clearShieldProperties();
        clearArmamentProperties();
        jBotShop.setName("");
        robotName.text = "";
        updateBotView();
    }

    // Clears all chassis properties
    function clearChassisProperties(){
        jBotShop.setCoins(jBotShop.getAmountChassis());
        jBotShop.setChassisSelected(false);
        jBotShop.setChassis("");
        jBotShop.setAmountChassis(0);
        jBotShop.setMaxWeight(0);
    }

    // Clears all shield properties
    function clearShieldProperties(){
        jBotShop.setCoins(jBotShop.getAmountShield());
        jBotShop.setShieldSelected(false);
        jBotShop.setShield("");
        jBotShop.setAmountShield(0);
        jBotShop.setWeightShield(0);
        jBotShop.setHealthOfShield(0);
    }

    //Clears all armament properties
    function clearArmamentProperties(){
        jBotShop.setCoins(jBotShop.getAmountArmament());
        jBotShop.setArmamentSelected(false);
        jBotShop.setArmament("");
        jBotShop.setAmountArmament(0);
        jBotShop.setWeightArmament(0);
        jBotShop.setFirePower(0);
    }

    init {
        clearAllSelectedItemsProperties();
        allRobots = jBotShop.getAllRobots();
        allChassis = jBotShop.getAllChassis();
        allShields = jBotShop.getAllShields();
        allArmaments = jBotShop.getAllArmaments();
        robotName.requestFocus();
    }

    var upperGroup : Group = Group{
        content: [
                robotListView,
                Group {
                    layoutX: 20
                    content : [
                        getNodeByName("currentBot_maxWeight"),
                        getNodeByName("currentBot_health"),
                        getNodeByName("currentBot_firePower"),
                        getNodeByName("currentBot_Price"),
                    ]
                }
                getNodeByName("Rahmen_currentBot"),
                getNodeByName("Rahmen_ownBots")
        ]
        layoutX: -30;
    }


    var upperLeftGroup : Group = Group{
        content: [
                getNodeByName("robotNameTextfield"),
                getNodeByName("robotNameText"),
                getNodeByName("currentCoins"),
                getNodeByName("coins"),
                coins,
                robotName,
        ]
        layoutX: -10;
        layoutY: 25;
    }


    // Group contains all components of the botshop to display
    var group : Group = Group {
            content: [
                getNodeByName("background"),
                getNodeByName("logo_small"),
                upperGroup,
                upperLeftGroup,
                getNodeByName("setChassis"),
                getNodeByName("setShield"),
                getNodeByName("setArmament"),
                getNodeByName("linie"),
                getNodeByName("logout"),
                Rectangle {
                    var fxzLayout = getNodeByName("logout").layoutBounds;
                    x : fxzLayout.minX;
                    y : fxzLayout.minY;
                    width : 80
                    height : 15
                    cursor : Cursor.HAND;
                    onMouseClicked : function( e : MouseEvent) : Void {
                        getPlayer().logout();
                        switchNode("Login");
                    }
                    opacity:0
                },
                Group {
                    content: [
                        showCurrentBotChassis,
                        showCurrentBotShield,
                        showCurrentBotArmament,
                        showCurrentPrice
                    ]
                    layoutX: -10
                }
                Group {
                    content: [ showCurrentRobot ]
                    layoutX: -10
                    layoutY: -5
                }
                chassisListView,
                shieldListView,
                armamentListView,
                Button { //save the bot
                    var fxzLayout = getNodeByName("button_buyBot").layoutBounds;
                    layoutX : fxzLayout.minX
                    layoutY : fxzLayout.minY
                    text: "buyBot"
                    action: function():Void {
                        jBotShop.setName(robotName.text as String);
                        if(jBotShop.checkParams()){
                            var tmp : String = jBotShop.submit();
                            if(tmp == null){
                                insert Message {
                                        msg: "Congratulations! You got a new bot."
                                        msgBoxBackground: getNodeByName("msgBoxmessageBox")

                                        } into group.content;
                                    chassisListView.select(-1);
                                    shieldListView.select(-1);
                                    armamentListView.select(-1);
                                    robotListView.select(-1);
                                    jBotShop.initialize();
                                    allRobots = jBotShop.getAllRobots();
                                    robotListView.items = jBotShop.getAllRobotsAsArray();
                                    robotName.text = "";
                            } else {
                                insert Message {
                                        msg: tmp
                                        msgBoxBackground: getNodeByName("msgBoxmessageBox")
                                        } into group.content;
                            }
                            clearAllSelectedItemsProperties();
                        } else{
                        insert Message {
                                msg: "Please check all components and name of your bot!"
                                msgBoxBackground: getNodeByName("msgBoxmessageBox")

                                } into group.content;
                        }
                    }
                    font: Constants.FONT_BUTTON
                    cursor : Cursor.HAND
                    width: 150
                    height : 30
                    skin : Constants.getButtonSkin();
                },
                Button { //cancel current mode
                    var fxzLayout = getNodeByName("button_cancelMode").layoutBounds;
                    layoutX : fxzLayout.minX
                    layoutY : fxzLayout.minY
                    text: "resetAll"
                    action: function():Void {
                        chassisListView.select(-1);
                        shieldListView.select(-1);
                        armamentListView.select(-1);
                        clearAllSelectedItemsProperties();
                    }
                    font: Constants.FONT_BUTTON
                    cursor : Cursor.HAND
                    width: 150
                    height : 30
                    skin : Constants.getButtonSkin();
                },
                Button { //cancel and go back to lobby
                    var fxzLayout = getNodeByName("button_leaveShop").layoutBounds;
                    layoutX : fxzLayout.minX
                    layoutY : fxzLayout.minY
                    text: "leaveShop"
                    action: function():Void {
                       switchNode("Lobby");
                    }
                    font: Constants.FONT_BUTTON
                    cursor : Cursor.HAND
                    width: 150
                    height : 30
                    skin : Constants.getButtonSkin();
                }
                Button { // sell bot button
                    var fxzLayout = getNodeByName("Rahmen_ownBots").layoutBounds;
                    layoutX: fxzLayout.minX -10
                    layoutY: fxzLayout.maxY -10
                    action: function():Void {
                        try {
                            var tmp = jBotShop.saleBot((allRobots.get(robotListView.selectedIndex) as java.util.ArrayList).get(5) as Integer);
                            if (tmp != ""){
                                 insert Message {
                                            msg: tmp
                                            msgBoxBackground: getNodeByName("msgBoxmessageBox")
                                 } into group.content;
                            } else {
                                robotListView.select(-1);
                                jBotShop.initialize();
                                 allRobots = jBotShop.getAllRobots();

                                 robotName.requestFocus();
                                robotListView.items = jBotShop.getAllRobotsAsArray();
                                clearAllSelectedItemsProperties();
                                showCoinsOfPlayer = jBotShop.getDBCoinsMinusItems().toString();
                                updateBotView();
                            }
                        } catch (ArrayIndexOutOfBoundsException) {
                            println("out of bounds")
                        }
                    }
                    text: "sell"
                    font: Constants.FONT_BUTTON
                    cursor : Cursor.HAND
                    width: 80
                    height : 20
                    skin: Constants.getButtonSkin();
                }
            ]
        };

    /**
    * override custom node and returns botShop Node
    */
    public override function create(): Node {
        return group;
    }
}
